/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年1月30日 下午3:02:56
 */
package com.lenovo.lmrp.server.itsmgateway.domain.engineer;

import java.util.Objects;

import com.lenovo.lmrp.api.model.entity.EngineerPO;
import com.lenovo.lmrp.api.model.entity.EngineerPO.EngineerStatus;
import com.lenovo.lmrp.api.model.view.EngineerVO;
import com.lenovo.lmrp.server.itsmgateway.adapter.AItsmAdapter;
import com.lenovo.sdc.framework.util.CommonUtil;
import com.lenovo.sdc.framework.util.Tuple;
import com.lenovo.sdc.framework.util.Tuple.Triplet;

/**
 * 
 * @author wujin
 * @version $Id: EngineerBO.java, v 0.1 2016年1月30日 下午3:02:56 wujin Exp $
 */
public class EngineerBO {
    private EngineerPO po;
    private String itsmCode;
    private boolean isdirty;
    
    private AItsmAdapter adapter;
    
    public EngineerBO(AItsmAdapter adapter, String itsmCode, EngineerPO po) {
        this.po = po;
        this.itsmCode = itsmCode;
        this.adapter = adapter;
    }
    
    public Tuple.Pair<Boolean, String> authenticate(String password) {
        Tuple.Pair<Boolean, String> response = adapter.authenticate(po.getLoginName(), password);
        if (response.getOne()) {
            // sync engineer data if not found in our platform DB
            if (Objects.isNull(po.getId())) {
                Triplet<Boolean, String, EngineerPO> newPO = adapter.getEngineer(po.getLoginName());
                if (newPO.getOne()) {
                    po.setId(newPO.getThird().getId());
                    po.setFullName(newPO.getThird().getFullName());
                    po.setEmail(newPO.getThird().getEmail());
                    po.setPhone(newPO.getThird().getPhone());
                    po.setSpId(newPO.getThird().getSpId());
                    po.setStatus(newPO.getThird().getStatus());
                    isdirty = true;
                }
            }
        }

        return response;
    }
    
    /**
     * only update non-null value
     * 
     * @param vo
     * @return
     */
    public Tuple.Pair<Boolean, String> update(EngineerVO vo) {
        if (CommonUtil.isNullOrEmpty(vo.getEmail()) || vo.getEmail().equals(po.getEmail()))
            vo.setEmail(null);
        else
            po.setEmail(vo.getEmail());
        
        if (CommonUtil.isNullOrEmpty(vo.getPhone()) || vo.getPhone().equals(po.getPhone()))
            vo.setPhone(null);
        else
            po.setPhone(vo.getPhone());
        
        if (CommonUtil.nonNullAndEmpty(vo.getPasswd()) && CommonUtil.nonNullAndEmpty(vo.getOldPassword()))
            vo.setPasswd(vo.getPasswd());
        
        if (vo.getStatus() == -1) {
            vo.setStatus(po.getStatus().getCode());
        }
        else {
            po.setStatus(EngineerStatus.values()[vo.getStatus()]);
        }
        isdirty = true;
        
        return adapter.updateEngineer(vo);
    }
    
    
    /**
     * expose PO will bring up risk by other invoker change field value. 
     * return a mutable object is good choice but it also cuase create new object.
     * 
     * Now, if invoker change PO field, please use repository save changed PO
     * 
     * @return
     */
    public void store(IEngineerRepository repository) {
        if (isdirty)
            repository.storeEngineer(itsmCode, po);
        
        isdirty = false;
    }
    
    public String getID() {
        return po.getId();
    }
    
    public EngineerVO getEngineerVO(){
        EngineerVO engineerVO = new EngineerVO();
        engineerVO.setEngineerid(po.getId());
        engineerVO.setLoginname(po.getLoginName());
        engineerVO.setName(po.getFullName());
        engineerVO.setPhone(po.getPhone());
        engineerVO.setEmail(po.getEmail());
        engineerVO.setStatus(po.getStatus().ordinal());
        engineerVO.setItsmcode(itsmCode);
        engineerVO.setProvinceName(po.getProvince());
        engineerVO.setCityName(po.getCity());
        engineerVO.setCounty(po.getCounty());
        return engineerVO;
    }
}
